//业务试题对象转组件试题数据
const old_to_new = (old_obj) => {
    //获取subtype函数
    const get_subtype = (review_type) => {
        const radio = ['单项选择题', '判断题']
        const checkbox = ['多项选择题', '不定项选择题']
        if (radio.includes(review_type)) {
            return 'radio'
        } else if (checkbox.includes(review_type)) {
            return 'checkbox'
        } else {
            return 'input'
        }
    }
    let {
        stem: question, parse: desc, solution: answer,
        answer: user_answer, typeCode: type_code, typeName: type_name, isCollect: is_collect,
        id, sort, topicID: topic_id, reviewType: review_type, parent_sort, parent_id,
    } = old_obj
    //生成choices
    let choices = []
    let options = old_obj.options ? JSON.parse(old_obj.options) : []
    for (const i in options) {
        choices.push({choice: ['A', 'B', 'C', 'D', 'E', 'F'][i * 1], text: options[i * 1]})
    }
    //标准答案转字符串
    answer = answer ? JSON.parse(answer).join('') : null
    user_answer = user_answer ? JSON.parse(user_answer).join('') : null
    //获取subtype
    let subtype = get_subtype(old_obj.reviewType)
    //获取typeName
    type_name = [null, "单项选择题", "多项选择题", "不定项选择题", "判断题", "填空题", "简答题", "论述题", "案例题", "综合题", "平台操作题"][type_code]
    let new_obj = {
        question, choices, desc, answer, user_answer, type_code, type_name,
        is_collect, id, sort, topic_id, review_type, subtype
    }
    //如果存在主题需要添加主题sort和主题ID
    if (parent_id) {
        new_obj['parent_id'] = parent_id
        new_obj['parent_sort'] = parent_sort
    }
    return new_obj
}

function filter(data){
    if(data['answerTypes']){
        for (let item of data.answerTypes){
            for (let children in item.answerLogs){
                if(item.answerLogs[children]['answerLogs'].length){
                    for (let son in item.answerLogs[children]['answerLogs']){
                        let arr = [];
                        for (let i in JSON.parse(item.answerLogs[children]['answerLogs'][son]['options'])){
                            let option = ['A、','B、','C、','D、'][i*1] +JSON.parse(item.answerLogs[children]['answerLogs'][son]['options'])[i];
                            arr.push(option)
                        }
                        item.answerLogs[children]['answerLogs'][son]['options'] = JSON.stringify(arr);
                    }
                }else{
                    let arr = [];
                    for (let i in JSON.parse(item.answerLogs[children]['options'])){
                        let option = ['A、','B、','C、','D、'][i*1] +JSON.parse(item.answerLogs[children]['options'])[i];
                        arr.push(option)
                    }
                    // console.log('arr',arr)
                    item.answerLogs[children]['options'] = JSON.stringify(arr);
                }
            }
        }
    }
    return data;
}

//API接口数据转subjects 返回一个对象{subjects:[...],leads:{...}}
function source_to_subjects(source) {
    let subjects = null
    let leads = {}
    filter(source)
    //扁平化取出answer_logs中的所有试题，以及子题，子题会加上parent_sort(主题sort)
    const flat_answer_logs = (answer_logs) => {
        let temp_li = []
        for (const i of answer_logs) {
            //没有子题
            if (!i.answerLogs.length) {
                temp_li.push(i)
            } else {//有子题
                // console.log(i.stem)
                for (const sub_i of i.answerLogs) {
                    leads[i.id] = i.stem
                    sub_i.parent_sort = i.sort
                    sub_i.parent_id = i.id
                    temp_li.push(sub_i)
                }
            }

        }
        return temp_li

    }
    //subjects排序函数
    const subjects_sort = (a, b) => {
        //优先按照题型排序,再按照sort排序
        if (a.type_code === a.type_code) {
            if (a.parent_sort === b.parent_sort) {
                return a.sort - b.sort
            } else {
                return a.parent_sort - b.parent_sort
            }
        } else {
            return a.type_code - b.type_code
        }
    }
    //源数据中的所有试题取出到subjects
    const _source_to_subjects = (source) => {
        let temp_subjects = []
        for (const answerTypes of source['answerTypes']) {
            const temp_li = flat_answer_logs(answerTypes['answerLogs'])
            temp_subjects = [...temp_subjects, ...temp_li]
        }
        //依次转化成组件数据
        for (const i in temp_subjects) {
            temp_subjects[i * 1] = old_to_new(temp_subjects[i * 1])
        }
        //排序
        temp_subjects.sort(subjects_sort)
        return temp_subjects

    }
    subjects = _source_to_subjects(source)
    return {
        subjects,
        leads
    }
}

export {old_to_new}
export default source_to_subjects
